package com.lc.question.controller;


import com.lc.question.constant.MyConstants;
import com.lc.question.pojo.*;
import com.lc.question.service.impl.PlanServiceImpl;
import com.lc.question.service.impl.QuestionNaireServiceImpl;
import com.lc.question.service.impl.QuestionServiceImpl;
import com.lc.question.service.impl.UserTabServiceImpl;
import com.lc.question.pojo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@Api(tags = "QuestionController|问题接口类")
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    QuestionServiceImpl questionService;
    @Autowired
    QuestionNaireServiceImpl questionNaireService;
    @Autowired
    UserTabServiceImpl userTabService;
    @Autowired
    PlanServiceImpl planService;

    @ApiOperation(value = "que_list(用户的问卷列表)")
    @ResponseBody
    @PostMapping("/que_list")
    public MyQuestion myQuestionNaire(int tab_Id){
        //问卷
        QuestionNaire questionNaire = questionNaireService.findQuestionNaireByTabId(tab_Id);
        List<Question> questions = questionService.questionList(tab_Id);
        Map<String,String[]> question_contents = new HashMap<>();
        for (Question question : questions) {
            String que_title = question.getQue_title();//问题标题
            if (question.getQue_type() != MyConstants.FILL_IN) {
//                String[] chooses = question.getQue_content().split(",");
                String[] chooses = question.getQue_content().split("%##%");
                question_contents.put(que_title,chooses);
            }else{
                String[] chooses = new String[1];
                chooses[0] = question.getQue_content();
                question_contents.put(que_title,chooses);
            }
        }

        //我的问卷
//        MyQuestion myQuestion = new MyQuestion(tab_title,tab_content,questions,question_contents);
        MyQuestion myQuestion = new MyQuestion(questionNaire,questions,question_contents);
        return myQuestion;

    }

    @ResponseBody
    @ApiOperation(value = "que_result(用户的问卷填写结果)")
    @PostMapping("/que_result")
    public Result questionResult(int tab_Id){
        //问卷
        QuestionNaire questionNaire = questionNaireService.findQuestionNaireByTabId(tab_Id);
        String tab_title = questionNaire.getTab_title();//问卷标题
        String tab_content = questionNaire.getTab_content();//问卷描述
        List<Question> questions = questionService.questionList(tab_Id);

        Map<String,Map<String,Integer>> question_results = new HashMap<>();

        for (Question question : questions) {
            String que_title = question.getQue_title();//问题标题
            int num_id = question.getNum_Id();
            int countAll = questionService.questionList2(tab_Id,num_id);//
            Map<String,Integer> map = new HashMap<>();
            if (!question.getQue_type().equals(MyConstants.FILL_IN)) {
                System.out.println(question.getQue_type());
//                String[] chooses = question.getQue_content().split(",");
                String[] chooses = question.getQue_content().split("%##%");
                for (String choose : chooses) {
                    int result = questionService.questionList3(choose, tab_Id, num_id);
//                    System.out.println(result+"---"+countAll);
                    int rate = 0;
                    if(countAll != 0){
                       rate = (result*100)/countAll ;
                    }
                    map.put(choose,rate);
                }
                question_results.put(que_title,map);
            }else{

            }
        }

        //我的问卷
        Result result = new Result(tab_title,tab_content,questions,question_results);
        return result;

    }

    @ResponseBody //用户填写答案
    @ApiOperation(value = "que_answer(用户的填写答案)")
    @PostMapping("/que_answer")
    public void questionAnswer(@RequestBody List<Question> questions){
        String user_Id =  "";
        int plan_id = 0;
        for (Question question : questions) {
            questionService.addQuestion(question);
            user_Id = question.getUser_Id();
            plan_id = question.getPlan_Id();
        }
        if (plan_id!=0){
            //填写完成后更改问卷状态
            userTabService.updateMyTab(user_Id,plan_id);
            Date date = new Date();
            System.out.println(date);
            userTabService.updateOverTime(user_Id,date);
        }
    }


    //查询问卷表-通过表名模糊查询
    @PostMapping("/tabByName")
    @ApiOperation(value = "查询问卷表-通过表名模糊查询")
    @ResponseBody
    public QuestionNaire findQuestionNaireByTitle2(String tab_title,String user_Id) {
        return questionNaireService.findQuestionNaireByTitle2(tab_title,user_Id);
    }

    @PostMapping("/deleteQueNaire")
    @ApiOperation(value = "删除问卷表及其相关问题")
    @ResponseBody
    public boolean deleteQueNaire(int tab_Id){
        List<Plan> plans = planService.findPlanByTid(tab_Id);
        int flag = 0;
        for (Plan plan : plans) {
            flag = (plan==null || plan.getPlan_state().equals("FINISHED") || plan.getPlan_state().equals("HAVE_CANCELED")) ? flag : flag++;
        }
        if(flag==0){
            userTabService.deleteUserTab(tab_Id);
            questionService.deleteQuestion(tab_Id);
            questionNaireService.deleteQuestionNaire(tab_Id);
            return true;
        }else {
            return false;
        }
    }

    @ApiOperation(value = "更新问卷表及其相关问题")
    @ResponseBody
    @PostMapping("/updateQueNaire")
    public boolean updateQueNaire(@RequestBody MyQuestion myQuestion){
        QuestionNaire questionNaire = myQuestion.getQuestionNaire();//问卷表
        List<Question> questions = myQuestion.getQuestions();//问卷表题

        int tab_Id = questionNaire.getTab_Id();
        List<Plan> plans = planService.findPlanByTid(tab_Id);
        int flag = 0;
        for (Plan plan : plans) {
            flag = (plan==null || plan.getPlan_state().equals("FINISHED") || plan.getPlan_state().equals("HAVE_CANCELED")) ? flag : flag++;
        }

        if(flag==0){//判断是否存在仍在执行中的任务--0代表没有
            //问卷
            questionNaireService.updateQuestionNaire(questionNaire);//更改表相关
            for (Question question : questions) {
                questionService.updateQuestion(question);//更新每个问题
            }
            return true;
        }else {
            return false;
        }
    }

    @ApiOperation(value = "新增问卷表及其相关问题")
    @PostMapping("/addQueNaire")
    public void addQueNaire(@RequestBody MyQuestion myQuestion){

        QuestionNaire questionNaire = myQuestion.getQuestionNaire();//问卷表
        List<Question> questions = myQuestion.getQuestions();//问卷表题
        questionNaireService.addQueNaire(questionNaire);//新增问卷
        for (Question question : questions) {
            questionService.addQuestion(question);//新增问卷的问题
        }

    }


}
